var app = getApp()
var verify = require('../../../../../utils/verify-token.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //var commodityPriceDetail = JSON.parse(options.commodityPriceDetail);
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
      commodityPriceId: options.commodityPriceId,
      commodityId: options.commodityId
    });
    this.getDetail();
  },
  getDetail() {
    var that = this;
    var token = app.globalData.token;
    var baseUrl = app.globalData.baseUrl;
    wx.request({
      url: baseUrl + 'vending/commodity_price/search/commodity_goods_aisle/info',
      method: "get",
      header: {
        'AUTHORIZATION': token
      },
      data: {
        commodityPriceId: that.data.commodityPriceId,
        commodityId: that.data.commodityId
      },
      success: function(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          var priceDetail = res.data.data;
          for (var key in priceDetail) {
            if (key.indexOf("Price") >= 0) {
              priceDetail[key] = priceDetail[key].toFixed(2);
            }
          }
          that.setData({
            priceDetail: priceDetail
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })

        }
      }
    })
  },

  // 修改商品显示名
  editShowName(res) {
    var commodityShowName = res.detail.value;
    var priceDetail = this.data.priceDetail;
    priceDetail.commodityShowName = commodityShowName
    this.setData({
      priceDetail: priceDetail
    })
  },

  // 修改商品优惠价格
  editBPrice(res) {
    var price = res.detail.value;
    if (!/^(\d?)+(\.\d{0,2})?$/.test(price)) { //正则验证，金额小数点后不能大于两位数字
      price = price.substring(0, price.length - 1);
    }
    var priceDetail = this.data.priceDetail;
    priceDetail.preferentialPrice = price;
    this.setData({
      priceDetail: priceDetail
    })
  },
  // 修改商品价格
  editBPrice1(res) {
    var price = res.detail.value
    if (!/^(\d?)+(\.\d{0,2})?$/.test(price)) { //正则验证，金额小数点后不能大于两位数字
      price = price.substring(0, price.length - 1);
    }
    var priceDetail = this.data.priceDetail;
    priceDetail.commodityPrice = price;
    this.setData({
      priceDetail: priceDetail
    })
  },



  // 保存全部参数
  save() {
    var commodityShowName = this.data.priceDetail.commodityShowName;
    var commodityPriceId = this.data.commodityPriceId;
    var preferentialPrice = this.data.priceDetail.preferentialPrice;
    var commodityPrice = this.data.priceDetail.commodityPrice;
    var commodityId = this.data.commodityId;
    var token = app.globalData.token;
    var baseUrl = app.globalData.baseUrl;
    var that = this;
    wx.request({
      url: baseUrl + 'vending/commodity_price/' + commodityPriceId + '/commodity_goods_aisle/update',
      method: "post",
      header: {
        'AUTHORIZATION': token,
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        commodityPriceId: commodityPriceId,
        commodityId: commodityId,
        commodityShowName: commodityShowName,
        commodityPrice: commodityPrice,
        preferentialPrice: preferentialPrice
      },
      success: function(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          wx.showToast({
            title: '保存成功!',
            duration: 3500
          })
          that.getDetail();
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  }
})